const App = {
    mixins: [Query],//配置文件，路径/utils/mixins.js
    data() {
        return {
            photo,
            page: {
                activeName: 'first',
                value1: '',
                PageNumber: 1, //页码
                RowAmount: 10,
                Condition: window.btoa(unescape(encodeURIComponent("%%"))),
                ExtensionCondition:  window.btoa(unescape(encodeURIComponent("AND personal_information.RR_Village_Number IN (" +  getCookie('village')  + ")\r\n")))
            },
            str1: '',
            str: '',
        }
    },
    mounted() {

    },
    methods: {
        Treedata() {
            const _this = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/clientOperation/getDistrictTree`,
                type: "GET",
                data: {
                    DistrictCode: '232723'

                },
                success: (data) => {
                    this.treeData = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        handleNodeClick(data, node) {
            this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + data.ProvinceNumber + "%,%" + this.page.value1 + "%")))
            this.postgetList(this.page);
        },
        check() {
            this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + this.page.value1 + "%")))
            this.postgetList(this.page);
        },
        handleEdit(row) {
            this.$confirm('此操作将确认兑换,是否继续? 确认兑换后将扣减商品库存。', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    method: "post",
                    data: {
                        Data: 'sales_order_detail',
                        Condition: `Sales_Order_SerialNumber='${row.Sales_Order_SerialNumber}'`,
                    },
                    success: (sales_order_detail) => {
                        let Article_Inventory_SerialNumber = sales_order_detail.map(i => "'" + i.Article_Inventory_SerialNumber + "'").join(",");
                        $.ajax({
                            headers: requestHeaders(),
                            url: `${mh}/generalBusiness/queryMultiTableByCondition`,
                            method: "post",
                            data: {
                                Data: JSON.stringify([{
                                    tableName: 'article_inventory',
                                    Condition: window.btoa(unescape(encodeURIComponent("Article_Inventory_SerialNumber IN (" + Article_Inventory_SerialNumber + ")"))),
                                },{
                                    tableName: 'sales_order',
                                    Condition: window.btoa(unescape(encodeURIComponent("Sales_Order_SerialNumber='" + row.Sales_Order_SerialNumber + "'"))),
                                }]),
                            },
                            success: ({ article_inventory,sales_order }) => {
                                const ls_Datetime = getYMDHMSDate();
                                let article_inventory_out = [];
                                sales_order_detail.forEach(item => {
                                    article_inventory_out.push({
                                        Article_Inventory_SerialNumber: item.Article_Inventory_SerialNumber,
                                        Article_SerialNumber: item.Article_SerialNumber,
                                        Specifications_SerialNumber: item.Specifications_SerialNumber,
                                        Inventory_Out_Datetime: ls_Datetime,
                                        Children: {
                                            article_inventory_out_detail: [{
                                                Article_Purpose: '商品被兑换',
                                                Inventory_Out_Quantity: item.Quantity
                                            }]
                                        }
                                    })
                                });
                                article_inventory.forEach(item => {
                                    let obj = sales_order_detail.filter(i => i.Article_Inventory_SerialNumber == item.Article_Inventory_SerialNumber)[0];
                                    item.Inventory_Quantity = accSub(item.Inventory_Quantity, obj.Quantity);
                                    item.Inventory_Out_Quantity = accAdd(item.Inventory_Out_Quantity, obj.Quantity);
                                })
                                sales_order[0].Row_Status = '0'
                                let lJSONObject_DataSet = {
                                    Add: {
                                        article_inventory_out,//出库单
                                    },
                                    Modify: {
                                        article_inventory,//回写库存
                                        sales_order
                                    }
                                };
                                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                                    Operation_Type: '0',
                                    Operation_Path: this.relativePath,
                                    Operation_Main,
                                    Operation_Interface: "generalSaveByRequestBody",
                                    Operation_IP: getIpAddress()
                                }]
                                submitSave(lJSONObject_DataSet, (res) => {
                                    if (res.Status == 'success') {
                                        this.postgetList(this.page);
                                        this.$message({
                                            message: '确认成功',
                                            type: 'success'
                                        });
                                    }
                                })
                            },
                            complete: (XMLHttpRequest) => {
                                requestPretreatment(XMLHttpRequest)
                            },
                        });
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest)
                    },
                });
            }).catch(() => { });
        },
        handleDelete(row) {
            this.$confirm('此操作将作废该兑换单? 是否继续', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/queryMultiTableByCondition`,
                    method: "post",
                    data: {
                        Data: JSON.stringify([{
                            tableName: 'personal_points_account',
                            Condition: window.btoa(unescape(encodeURIComponent("ID_Card_Number='" + row.ID_Card_Number + "'"))),
                        },{
                            tableName: 'sales_order',
                            Condition: window.btoa(unescape(encodeURIComponent("Sales_Order_SerialNumber='" + row.Sales_Order_SerialNumber + "'"))),
                        }]),
                    },
                    success: ({personal_points_account,sales_order}) => {
                        let personal_points_change_log = [];
                        //退还积分账户
                        let New_Villager_Remain_Points, New_Party_Remain_Points;
                        personal_points_account.forEach(item => {
                            if (item.Points_Type == '0') {
                                item.Remain_Points = accAdd(item.Remain_Points, row.Sales_Villager_Point);
                                item.Sales_Points = accSub(item.Sales_Points, row.Sales_Villager_Point);
                                New_Villager_Remain_Points = accAdd(item.Remain_Points, row.Sales_Villager_Point);
                            } else if (item.Points_Type == '1') {
                                item.Remain_Points = accAdd(item.Remain_Points, row.Sales_Party_Point);
                                item.Sales_Points = accSub(item.Sales_Points, row.Sales_Party_Point);
                                New_Party_Remain_Points = accAdd(item.Remain_Points, row.Sales_Party_Point);
                            }
                        });
                        //生成日志
                        if (row.Sales_Villager_Point > 0) {
                            personal_points_change_log.push({
                                ID_Card_Number: row.ID_Card_Number,
                                Points_Title: '兑换商品取消退还村民积分',
                                Points_Type: '0',
                                Change_Points: row.Sales_Villager_Point,
                                Remain_Points: New_Villager_Remain_Points,
                                Notes: '兑换商品取消退还村民积分'
                            })
                        }
                        if (row.Sales_Party_Point > 0) {
                            personal_points_change_log.push({
                                ID_Card_Number: row.ID_Card_Number,
                                Points_Title: '兑换商品取消退还党员积分',
                                Points_Type: '1',
                                Change_Points: row.Sales_Party_Point,
                                Remain_Points: New_Party_Remain_Points,
                                Notes: '兑换商品取消退还党员积分'
                            }) 
                        }
                        sales_order[0].Row_Status = '1';
                        let lJSONObject_DataSet = {
                            Add: {
                                personal_points_change_log,//出库单
                            },
                            Modify: {
                                personal_points_account,//回写库存
                                sales_order
                            }
                        };
                        let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                        lJSONObject_DataSet['Add']['base_operation_log'] = [{
                            Operation_Type: '0',
                            Operation_Path: this.relativePath,
                            Operation_Main,
                            Operation_Interface: "generalSaveByRequestBody",
                            Operation_IP: getIpAddress()
                        }]
                        submitSave(lJSONObject_DataSet, (res) => {
                            if (res.Status == 'success') {
                                this.postgetList(this.page);
                                this.$message({
                                    message: '确认成功',
                                    type: 'success'
                                });
                            }
                        })
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest)
                    },
                });
            }).catch(() => { });
        }
    },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
